package com.aatrox.bhsypro.controller;

import com.aatrox.bhsypro.dao.BhSyShopDao;
import com.aatrox.bhsypro.entity.*;
import com.aatrox.bhsypro.service.*;
import com.aatrox.bhsypro.util.CookieUtil;
import com.aatrox.bhsypro.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/bhsy")
@SessionAttributes({"bUser"})
public class BhSyProController {

    @Autowired
    BhSyGosService bgs;
    @Autowired
    BUserService bus;
    @Autowired
    BUserService bUserService;
    @Autowired
    BVideoService bvs;
    @Autowired
    protected RedisTemplate<String,Object> redisTemplate;


    /**
     * 跳转首页
     */
    @RequestMapping("/Bmain")
    public String bMain(String name, String pass, HttpServletRequest request, HttpServletResponse response, ModelMap mm,HttpSession session) throws IOException {
        BUser bUser=bUserService.dl(new BUser(null,name,pass),request,response);
        session.setAttribute("buserId",bUser.getUid());
        mm.put("bUId",bUser.getUid());
        mm.put("locUser",bUser);
        return "txt/BMain";
    }

    /**
     * 跳转登录页
     * pass接收注册密码
     */
    @RequestMapping("/login")
    public String login(){

        return "txt/BLogin";
    }

    /**
     * 跳转视频页面
     */
    @RequestMapping("/Bvideo")
    public String bVideo(){

        return "txt/BVideo";
    }

    /**
     * 跳转专栏
     * @return
     */
    @RequestMapping( "/zl")
    public String zl(HttpSession session,ModelMap mm,HttpServletRequest request){
        String ticket = CookieUtil.getCookieValue(request,"userTicket");
        BUser user = (BUser) redisTemplate.opsForValue().get("user:"+ticket+"uid:"+(session.getAttribute("buserId")));
        BhSyGos bg = bgs.selIsGos(user.getUid());
        System.out.println("商家状态:---"+bg);
        if (bg==null){
            mm.put("Gos",0);
        }else if (bg!=null&&bg.getSq()==0){
            mm.put("Gos",1);
        }else {
            mm.put("Gos",2);
        }
        mm.put("bUserId",user.getUid());
        return "txt/FuWenBeng";
    }
    @Autowired
    UAttentionService uts;
    @Autowired
    BhSyHrService bshs;
    /**
     * 跳转视频页
     */
    @RequestMapping("/video/{uid}/{vid}")
    public String video(@PathVariable("uid") int uid, @PathVariable("vid") int vid,ModelMap mm,HttpServletRequest request,HttpSession session){
        String ticket = CookieUtil.getCookieValue(request,"userTicket");
        BUser Buser = (BUser) redisTemplate.opsForValue().get("user:"+ticket+"uid:"+(session.getAttribute("buserId")));
        BUser user = bus.selUserForUid(uid);
        BVideo video = bvs.selVideoForVid(vid);
        HashMap hm = new HashMap();
        hm.put("vuid",video.getUid());
        hm.put("uid",Buser.getUid());
        UAttention uAttention = uts.selUAttentionForVuid(hm);
        hm.clear();
        //添加历史记录
        BhSyHr bsh = new BhSyHr(Buser.getUid(),vid,1);
        bshs.insertNewHr(bsh);
        if (uAttention == null){
            mm.put("uation",0);
        }else {
            mm.put("uation",1);
        }
        mm.put("bVideo",video);
        mm.put("vUser",user);
        mm.put("bvType",1);
        return "/realuser/WatchVideo";
    }

    @Autowired
    BhSyShopDao bssd;

    /**
     * 购物
     */
    @RequestMapping("/byShop")
    public String byShop(HttpServletRequest request,ModelMap mm,HttpSession session){
        List<BhSyShop> list = bssd.selAllShop();
        Date startDate = null;
        Date nowDate = new Date();
        for (BhSyShop bss:list) {
            startDate = bss.getStartDate();
            Long sD = startDate.getTime();
            Long nD = nowDate.getTime();
            bss.setLastTime(sD-nD);
            bss.setEndTime(bss.getEndDate().getTime());
        }
        String ticket = CookieUtil.getCookieValue(request,"userTicket");
        BUser user = (BUser) redisTemplate.opsForValue().get("user:"+ticket+"uid:"+(session.getAttribute("buserId")));
        System.out.println("{全部商品}"+list.toString());
        mm.put("allShop",list);
        return "txt/BbyShop";
    }
    /**
     * 支付宝跳转
     * @return
     */
    @RequestMapping(value = "/byBv")
    public String byBv (){

        return "alipay/BuyVip";
    }

    /**
     * 购买大会员
     */
    @RequestMapping("/byVip")
    public String byVip(HttpServletRequest request,ModelMap mm,HttpSession session){
        String ticket = CookieUtil.getCookieValue(request,"userTicket");
        BUser user = (BUser) redisTemplate.opsForValue().get("user:"+ticket+"uid:"+(session.getAttribute("buserId")));
        mm.put("uid",user.getUid());
        return "txt/ByHuiYua";
    }
    /**
     * 跳转搜索
     * @return
     */
    @RequestMapping("/Bsearch")
    public  String bsearch1(){
        return "realuser/Bsearch";
    }
    @ResponseBody
    @RequestMapping("/ss")
    public JsonResult bsearch(@RequestParam(name = "sname",defaultValue="") String sname){
        List list=bScolumnService.selbysname(sname);
        System.out.println(list);
        for (int i=0;i<list.size();i++){
//            Object b=
            BScolumn2 b2= (BScolumn2) list.get(i);
            String img=b2.getSImg().split(",")[0];
//            String msg=b2.getSMsg().replaceAll("[^\u4E00-\u9FA5]", "");
//            b2.setSMsg(msg);
            b2.setSImg(img);
            System.out.println(b2);
        }
        HashMap hs=new HashMap();
        hs.put("list",list);
        return new JsonResult(200,"成功",hs);
    }



    /**
     * 跳转专栏
     * @return
     */
    @Autowired
    BScolumnService bScolumnService;

    @RequestMapping("/BColumn2/{stay}")
    public String BCoulumn (ModelMap map,@PathVariable("stay") String stay){
        List list=bScolumnService.selallbystay(stay);
        List list2=bScolumnService.selScolumby10();
        for (int i=0;i<list.size();i++){
//            Object b=
            BScolumn2 b2= (BScolumn2) list.get(i);
            String img=b2.getSImg().split(",")[0];
//            String msg=b2.getSMsg().replaceAll("[^\u4E00-\u9FA5]", "");
//            b2.setSMsg(msg);
            b2.setSImg(img);
            System.out.println(b2);
        }

        map.put("list",list);
        map.put("list2",list2);
        return "txt/BColumn";
    }


    @RequestMapping("/BColumn")
    public String BCoulumn (ModelMap map){
        List list=bScolumnService.selallScolum();
        List list2=bScolumnService.selScolumby10();
        for (int i=0;i<list.size();i++){
//            Object b=
            BScolumn2 b2= (BScolumn2) list.get(i);
            String img=b2.getSImg().split(",")[0];
//            String msg=b2.getSMsg().replaceAll("[^\u4E00-\u9FA5]", "");
//            b2.setSMsg(msg);
            b2.setSImg(img);
            System.out.println(b2);
        }

        map.put("list",list);
        map.put("list2",list2);
        return "txt/BColumn";
    }

    /**
     * 跳转管理页面
     */
    @RequestMapping("/Bcontroller")
    public String bController(HttpServletRequest request,ModelMap mm,HttpSession session){
        String ticket = CookieUtil.getCookieValue(request,"userTicket");
        BUser user = (BUser) redisTemplate.opsForValue().get("user:"+ticket+"uid:"+(session.getAttribute("buserId")));
        List<BhSyGos> list = bgs.selAllGos();
        List<BUser> uList = new ArrayList<>();
        System.out.println("申请用户结果:"+list);
        for (BhSyGos bg:list) {
            uList.add(bus.selUserForUid(bg.getUid()));
        }
        System.out.println(uList);
        mm.put("uList",uList);
        return "realuser/adminController";
    }

    @Autowired
    UCollectService ucs ;
    /**
     * 跳转个人首页
     */
    @RequestMapping("/bMyself")
    public String bMyself(HttpServletRequest request,ModelMap mm,HttpSession session){
        String ticket = CookieUtil.getCookieValue(request,"userTicket");
        BUser user = (BUser) redisTemplate.opsForValue().get("user:"+ticket+"uid:"+(session.getAttribute("buserId")));
        List<UCollect> list = ucs.selAllCollectForUid(user.getUid());
        mm.put("bMUser",user);
        if (list == null){
            mm.put("cstatic",1);
        }else {
            mm.put("cstatic",2);
            mm.put("allCollect",list);
        }
        BMySelf bm = bvs.selVideoSum(user.getUid());
        if (bm == null){
            mm.put("bmVideo",1);
        }else {
            mm.put("bmVideo",2);
            mm.put("bVideoSum",bm);
        }
        return "txt/BMyself";
    }


    /**
     * 跳转图片限制大小尺寸
     */
    @RequestMapping("/imgsize")
    public String imgsize(){
        return "realuser/imgsize";
    }

    //---------------------------------------------------------
    @RequestMapping("/theMain")
    public String theMain(HttpServletRequest request,ModelMap mm,HttpSession session){
        String ticket = CookieUtil.getCookieValue(request,"userTicket");
        BUser user = (BUser) redisTemplate.opsForValue().get("user:"+ticket+"uid:"+(session.getAttribute("buserId")));
        mm.put("bUId",user.getUid());
        mm.put("locUser",user);
        return "txt/BMain";
    }
    /**
     * 跳转直播间
     *
     */
    @RequestMapping("/zhiBoH")
    public String zhiBoH(HttpServletRequest request,ModelMap mm,HttpSession session ){
        String ticket = CookieUtil.getCookieValue(request,"userTicket");
        String zhiBoAllUid = (String) redisTemplate.opsForValue().get("ZhiBoV");
        System.out.println("{直播间:}"+zhiBoAllUid);
        ArrayList<BUser> list = new ArrayList<BUser>();
        if (zhiBoAllUid==null){
            mm.put("zbAll",null);
        }else {
            String zbAll[] = zhiBoAllUid.split(",");
            for (int i = 0; i < zbAll.length ; i++) {
               list.add(bus.selUserForUid(Integer.parseInt(zbAll[i])));
            }
            mm.put("zbAll",list);
        }
        return "txt/ZhiBoHouse";
    }
}
